

//检测手机格式
function checkPhone(phone){
	var phone_match=/^[1][3458][0-9]{9}$/;
	if(phone_match.test(phone)){
		return true;
	}else{
		return false;
	}
}

//检测账号格式
function checkUsername(username)
{
	var RegCellPwd = /^[0-9A-Za-z_]{6,32}$/;
	if(username == 'admin'){
		RegCellPwd = /^[0-9A-Za-z_]{5,32}$/;
	}
	if(RegCellPwd.test(username)){
		return true;
	}else{
		return false;
	}
}
var max_count_time = 180
var count_time 	   = max_count_time ;
var idInt	       = null ;

//计算剩余时间
function count_surplus_time(){
	if(count_time === max_count_time){
    	$("#surplus_time_part").css({"display":""});
    	$("#send_verify_num").css({"display":"none"})
	}
	
	$("#surplus_time").html(count_time);
	
	
    if(count_time <= 0){
    	clearInterval(idInt);
    	count_time = max_count_time;
    	$("#surplus_time_part").css({"display":"none"});
    	$("#send_verify_num").css({"display":""});
    	
    }else{
    	count_time --;
    }
    
}





/*************   查找密码 开始   *******************************/
$(document).ready(function(){
    $('.reg_input').bind('keydown',enterEvent("#find_pwd_btn"));

    //查找密码
	$("#find_pwd_btn").click(function(){
		var url='index.php?ct=user&ac=check_phone_verify';
		var username=$("#username").val();
		var phone=$("#phone").val();
		var verify_num=$("#verify_num").val();

	    if(username==""){
			alert("账号不能为空");
			$("#username").focus();
			return;
		}else if(!checkUsername(username)){
		    alert("请输入正确的账号格式!");
		    $("#username").focus();
		    return;
		}else if(phone==""){
			alert("手机不能为空!");
			$("#phone").focus();
			return;
		}else if(!checkPhone(phone)){
		    alert("请输入正确的手机格式!");
		    $("#phone").focus();
		    return;
		} else if(verify_num==""){
		    alert("验证码不能为空!");
		    $("#verify_num").focus();
		    return;
		}


        $.ajax({
			type: "post",
		    url: url,
			data:"verify_num="+verify_num+"&phone="+phone+"&username="+username,
			error:function(d,t){
				alert("系统繁忙");
			},
			dataType:"json",
			success: function(data){
				//alert(data);
				if(data.error == 0){
				    //alert('yes');
					$("#myform").submit();
				}else{
				    alert(data.tips);
				    if(data.error==1){
				        $("#verify_num").focus();
				    }else if(data.error==2){
				    	$("#phone").focus();
				    }
					//alert("请输入正确的验证码!");
				}
		    }

		});

	});
	

});
/*************   查找密码 结束   *******************************/






/********************注册页面   开始************************/

//是否可以使账号
var isCanUseUserName = false;
//是否可以使用邮件
var isCanUseEmail    = false;
//是否可以使用昵称
var isCanNickname    = false;

//验证码是否正确
var isVerifySuc 	 = false;

function submit_register()
{
	//alert(isCanUseUserName);
	var agreen = $("#agreen").is(':checked');
	var userName = $('#username').val();
	var pwd = $('#usrpwd').val();
	var second_pwd = $('#usrpwdc').val();
	var verifycode = $('#verifycode').val();

	//错误提示
	var tips='';
	//alert(isCanUseUserName);
	//alert(isCanUseEmail);
	//alert(agreen);
	//alert(pwd);
	//alert(second_pwd);
    if(userName == '' || userName.length == 0){
		tips="账号不能为空！";
		$('#username').focus();
		return false;
	}else if(isCanUseUserName==false){
		//checkUserName();
		tips="账号有误";
		$('#username').focus();
		return false;
	}
	if (pwd == ''|| pwd.length == 0) {
		tips="密码不能为空！";
		ChkPwd();
		$('#usrpwd').focus();
		return false;
	}
	if(!(pwd.length>=6 && pwd.length<=16)){
		tips="密码的长度要在6-16之间！";
		ChkPwd();
		$('#usrpwd').focus();
		return false;
	}
	if (pwd != second_pwd) {
		tips="两次密码输入不一致";
		ChkPwdc();
		$('#usrpwdc').focus();
		return false;
	}
	
	if(!checkNickname()){
		tips="昵称有误";
		$("#nickname").focus();
		return false;
	}
	
	if(!checkEmail()){
		//checkEmail();
		tips="邮件有误";
		$("#email").focus();
		return false;
	}
	
	if(verifycode == '' || verifycode.length == 0){
		tips="验证码不能为空！";
		$('#verifycode').focus();
		return false;
	}
	if(isVerifySuc === false){
		tips="验证码有误";
		$("#verifycode").focus();
		return false;
	}
    
    
    if (agreen != true && tips=="") {
		tips="请阅读用户协议！";
		$("#agreen").focus();
	}

	if(tips==''){
		//alert('可以提交了');
		var bl = confirm("请确认无误提交","Yesno");
		if(bl){
			$("#myform").submit();
		}
	}else{
		alert(tips);
	}
}

/*
function key_login()
{

	if (event.keyCode==13)   //回车键的键值为13
		submit_register();
}*/


function checkEmail(){
    var mail_str=$("#email").val();
    if(mail_str==""){
        ShowMsg("emailspan",0,"你的电子邮箱地址是什么？");
		jQuery("#emailspan").attr("class","error");
        return false;
    }

    if(mail_str.match(/^[-_.a-z0-9A-Z]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([-_.][a-zA-Z0-9]+))+$/i)){    ///(^[a-zA-Z0-9_-]{1,})+@([a-zA-Z0-9]{1,})+\.([a-zA-Z]{2,})$/g)){
        // alert("yes");
        //ShowMsg("emailspan",1,"可以通过此邮箱修改密码。");
		var url = registerInfo;
        $.ajax({
				  type: "post",
				  url: url,
				  async: false,
				  data:"do=email&email=" + mail_str,
				  dataType:"json",
				  error:function(d,t){
        		  	ShowMsg("emailspan",0,"网络错误");
        		  	isCanUseEmail=false;
				  },
				  success: function(data){
					  	if(data == 1){
							isCanUseEmail = true;
							$('#emailspan').hide();
							ShowMsg("emailspan",1,"邮件确认无误。");
							jQuery("#emailspan").attr("class","ok");
		
					  	}else if (data == 2){
					  		$("#emailspan").html("该邮件已经被注册");
					  		isCanUseEmail = false;
					  	}
				  }
		});
    }else{
    	isCanUseEmail=false;
		jQuery('#emailspan').show();
		ShowMsg("emailspan", 0, "不是有效的电子邮箱。");
		jQuery("#emailspan").attr("class", "error");
    	//ShowMsg("emailspan",0,"不像是有效的电子邮箱。");
    }
    return isCanUseEmail;
}

//昵称检查
function checkNickname(){
	
    var nickname=$("#nickname").val();
    if(nickname==""){
        ShowMsg("nicknamespan",0,"请填写昵称！");
		jQuery("#nicknamespan").attr("class","error");
        return false;
    }

   if(nickname.length >=3 && nickname.length <= 15){  

		var url = registerInfo;
        $.ajax({
				  type: "post",
				  url: url,
				  async: false,
				  data:"do=nickname&nickname=" + nickname,
				  dataType:"json",
				  error:function(d,t){
        		  	ShowMsg("nicknamespan",0,"网络错误");
        		  	isCanNickname=false;
				  },
				  success: function(data){
					  	if(data == 1){
					  		isCanNickname = true;
					  		$('#nicknamespan').hide();
					  		ShowMsg("nicknamespan",1,"昵称确认无误。");
					  		jQuery("#nicknamespan").attr("class","ok");
					  	}else if (data == 2){
							jQuery('#nicknamespan').show();
							ShowMsg("nicknamespan", 0, "该昵称已经被使用");
							jQuery("#nicknamespan").attr("class", "error");
					  		isCanNickname = false;
							return false;
					  	}
				  }
		});

    }else{
    	isCanNickname=false;
		jQuery('#nicknamespan').show();
		ShowMsg("nicknamespan", 0, "昵称由3~15个字符组成。");
		jQuery("#nicknamespan").attr("class", "error");
		return false;
    }
    return isCanNickname;
}


function checkUserName() {
		var username = $('#username').val();
		if(checkUsername(username)) {
			var url = registerInfo;
			
			$.ajax({
				  type: "post",
				  url: url,
				  data:"do=username&username="+username,
				  error:function(d,t){
				  	isCanUseUserName=false;
				  	jQuery('#unspan').show();
				  	//$("#unspan").html("网络错误");
				  	ShowMsg("unspan", 0, "网络错误");
				  	jQuery("#unspan").attr("class", "error");
				  },
				  dataType:"json",
				  success: function(data){
				  	
					  	if(data == 1){
					  		isCanUseUserName = true;
					  		$('#unspan').hide();
					  		ShowMsg("unspan",1,"账号确认无误。");
					  		jQuery("#unspan").attr("class","ok");

					  	}else if (data == 2){
					  		//$("#unspan").html("该账号已经被注册");
							jQuery('#unspan').show();
							ShowMsg("unspan", 0, "该账号已经被注册");
							jQuery("#unspan").attr("class", "error");
					  		isCanUseUserName = false;
					  	}
				  }});
		} else {
			jQuery('#unspan').show();
			ShowMsg("unspan", 0, "帐号由6至32位字母、数字或_组成。");
			jQuery("#unspan").attr("class", "error");
			isCanUseUserName=false;
		    return false;
		}
	}


function checkVerifycode(){
	var verifycode = $("#verifycode").val();
	if(verifycode == '' || verifycode.length == 0){
		isVerifySuc=false;
		jQuery('#codespan').show();
		ShowMsg("codespan", 0, "验证码不可以为空");
		jQuery("#codespan").attr("class", "error");
	}else{
		var url = registerInfo;
		$.ajax({
			  type: "post",
			  url: url,
			  data:"do=code&code="+verifycode,
			  error:function(d,t){
				isVerifySuc=false;
			  	jQuery('#codespan').show();
			  	//$("#unspan").html("网络错误");
			  	ShowMsg("codespan", 0, "网络错误");
			  	jQuery("#codespan").attr("class", "error");
			  },
			  dataType:"json",
			  success: function(data){
				     //alert(data);

				  	if(data == 1){
				  		isVerifySuc = true;
				  		$('#codespan').hide();
				  		ShowMsg("codespan",1,"正确");
				  		jQuery("#codespan").attr("class","ok");
				  	}else if (data == 2){
				  		//$("#unspan").html("该账号已经被注册");
						jQuery('#codespan').show();
						ShowMsg("codespan", 0, "验证码错误");
						jQuery("#codespan").attr("class", "error");
						isVerifySuc = false;
				  	}
			  }});
	}
}






$(document).ready(function(){
	$("#register_btn").bind('click',submit_register);
	

	$("#verifycode_change").click(function(){
		isVerifySuc = false;
		$("#verifycode_img").attr("src","index.php?ct=user&ac=gen_verifycode&t=" +Math.random());
	});
	//$("#verifycode_change").click();
	
	
})

//身份证号码验证

function checkIdcard(){
	var idcard = $("#idcard").val();
    if(idcard==""){
        ShowMsg("idcardspan",0,"请输入您的身份证号码!");
		jQuery("#idcardspan").attr("class","error");
        return false;
    }
    idcard = idcard.toUpperCase();
    if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(idcard))){  
		jQuery('#idcardspan').show();
		ShowMsg("idcardspan", 0, "输入的身份证号长度不对，或者号码不符合规定！\n15位号码应全为数字，18位号码末位可以为数字或X。");
		jQuery("#idcardspan").attr("class", "error");  
        return false;  
   }  
	//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。  
	//下面分别分析出生日期和校验位  

	var len, re;  
	len = idcard.length; 
	
	if (len == 15){  
	re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
	
	var arrSplit = idcard.match(re);
	//检查生日日期是否正确  
	
	var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);  
	var bGoodDay;  
	bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));  
	 
	if (!bGoodDay){  
		jQuery('#idcardspan').show();
		ShowMsg("idcardspan", 0, "输入的身份证号里出生日期不对！");
		jQuery("#idcardspan").attr("class", "error");	 
	    return false;  
	}else{  
	//将15位身份证转成18位  
	//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。  
    var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  
	 var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');  
	 var nTemp = 0, i;    
	 idcard = idcard.substr(0, 6) + '19' + idcard.substr(6, num.length - 6); 
	
	 for(i = 0; i < 17; i ++){  
	      nTemp += idcard.substr(i, 1) * arrInt[i];  
	 }  
	 idcard += arrCh[nTemp % 11];    
		jQuery('#idcardspan').show();
		ShowMsg("idcardspan", 0, "身份证确认无误。");
		jQuery("#idcardspan").attr("class", "ok");     
	}   
	
} 
    
if (len == 18){  
	re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);  
	var arrSplit = idcard.match(re); 
	//检查生日日期是否正确  
	var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);  
	var bGoodDay;  
	bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));  
	if(!bGoodDay){  
		jQuery('#idcardspan').show();
		ShowMsg("idcardspan", 0, "输入的身份证号里出生日期不对！");
		jQuery("#idcardspan").attr("class", "error");
		return false;  
	}else{  
		//检验18位身份证的校验码是否正确。  
		//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。  
		var valnum;  
		var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);  
		var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');  
		var nTemp = 0, i;  
		for(i = 0; i < 17; i ++){  
			nTemp += idcard.substr(i, 1) * arrInt[i];  
		}  
	valnum = arrCh[nTemp % 11];  
	if (valnum != idcard.substr(17, 1)){  
		jQuery('#idcardspan').show();
		ShowMsg("idcardspan", 0, "请输入正确的身份证号码！");
		jQuery("#idcardspan").attr("class", "error"); 
	return false;  
	}  
	jQuery('#idcardspan').show();
	ShowMsg("idcardspan", 0, "确认身份证无误！");
	jQuery("#idcardspan").attr("class", "ok"); 
	}  
}
return true; 
}

//姓名验证
function checkTruename(){
	var truename = $("#truename").val();
	var website = $("#website").val();
    if(truename==""){
        ShowMsg("truenamespan",0,"请输入姓名！");
		jQuery("#truenamespan").attr("class","error");
        return false;
    }

   if(truename.length >=2 && truename.length <= 4){  

		var url = registerInfo;
        $.ajax({
				  type: "post",
				  url: url,
				  async: false,
				  data:"do=truename"+"&truename=" + truename,
				  dataType:"json",
				  error:function(d,t){
        		  	ShowMsg("truenamespan",0,"网络错误");
        		  	isCanTruename=false;
				  },
				  success: function(data){
					  	if(data == 1){
					  		isCanTruename = true;
					  		$('#truenamespan').hide();
					  		ShowMsg("truenamespan",1,"姓名确认无误。");
					  		jQuery("#truenamespan").attr("class","ok");
					  	}else if (data == 2){
							jQuery('#truenamespan').show();
							ShowMsg("truenamespan", 0, "该名已经被使用");
							jQuery("#truenamespan").attr("class", "error");
					  		isCanTruename = false;
							return false;
					  	}
				  }
		});

    }else{
    	isCanTruename=false;
		jQuery('#truenamespan').show();
		ShowMsg("truenamespan", 0, "姓名不正确！");
		jQuery("#truenamespan").attr("class", "error");
		return false;
    }
    return isCanTruename;
}



/********************注册页面   结束************************/

//注册页面  和   登陆页面    开始
$(document).ready(function(){
	$("#login_btn").click(function(){
		var tips="";
		var usernameObj=$("#login_username");
		var passwordObj=$("#login_password");
		var username=usernameObj.val();
		if(usernameObj.val()=="" && tips==""){
			usernameObj.focus();
			tips="账号不能为空";
		}


		if(!checkUsername(username) && tips==""){  
			usernameObj.focus();
			tips="账号必须在6-32位之间，只能包含字母、数字、下划线!";
		}

		if(passwordObj.val()=="" && tips==""){
			passwordObj.focus();
			tips="密码不能为空";
		}

		if(tips==""){
			$("#login_form").submit();
		}else{
			alert(tips);
		}
	});

	$('.login_input').bind('keydown',enterEvent("#login_btn"));
	$('.new_txt').bind('keydown',enterEvent("#register_btn"));
	//$('.login_input').bind('keydown',enterEvent("#login_form"));


})
//注册页面  和   登陆页面    结束


function  enterEvent(form_id_str,type){
    
    if(type=='function'){
   		return function(e){
    		var e=e || event;
			var keycode=e.which || e.keyCode;
			if(keycode==13){
				form_id_str();
			}
   		 }

    }else{
   		return function(e){
        	var alt=$(this).attr('alt');
        	if(alt=="disableEnter") return;
        	//alert($(this).attr('title'));
    		var e=e || event;
			var keycode=e.which || e.keyCode;
			if(keycode==13){
				$(form_id_str).click();
			}
   		 }

    }
}

//论坛登录
function submit_luntan()
{
	//错误提示
	var tips='';
	if(!checkNickname()){
		tips="昵称有误";
		$("#nickname").focus();
	}
   
	if(tips==''){
			$("#nickform").submit();
	}else{
		return false;
	}
}
